In today's lecture we are going to finish off coding the shop now.
Did save the shop is a challenging script and lots of things to remember.
And because we're using the for loop, what would be more simple we're now having to do for all these
items at once.
Um, but I hope you're following along okay with it because we're almost there.
We've only now got to work on this issue we had here with how it didn't display the stats at the start.
Um, and then we're purchasing the products.
So the issue with, with, with displaying the stats at the start is very simple to fix.
Actually, all we need to do is if the start of our script to write down some starting values, these
are the values that these will have at the start.
And this will be just be the values of the first item.
So um, we just need to go up to here to the top and create a section under here.
Called starting values.
So at the start, obviously we want the shop frame.
Dot visible to be false.
And the items remained visible to be true.
All right.
Shot frame.
Dot items.
Dot visible.
Sequel to True The Items frame.
Dot info.
Dot name label.
Dot text, so the name of it is going to be equal to shop items dot items.
First one.
Dot name like that's there.
I'm going to add in our viewport camera and have to do all that stuff again.
So local viewport camera is equal to instance dot new camera into um items frame.
Dot info.
Dot viewport frame viewport camera so no items frame.info dot viewport frame.
Dot current camera is equal to the viewport camera there.
Um local viewport object is equal to the shop items dot items.
The first one it's tool and clone that tool.
Then give it its parent.
So the viewport object dot parent is then going to be equal to the viewport camera.
We have done this a couple times now.
Um, but yeah.
When we see frame bits of the camera.
So viewport camera um, not object viewport camera dot frame is equal to see frame dot look at.
The acquisition is going to be the viewport object.
Dot primary part.
Dot position plus a vector three.
And this should be the same as what you included the other times.
So this is going to be 002 for me.
But if you change it last time it'll be different for you.
And then the ATS is going to be the viewport object dot primary part dot position.
We then want to put in the stats.
So for the stats we're going to be writing out the items frame.info, dot speed, label dot text is
equal to the speed here.
Like that.
Dot dot.
To string the shop items.
Dot items first one dot speed.
Now you could ask, well, I know the speed here is ten or whatever.
Why don't I just put this as ten?
Well, it's bad practice because if you then go ahead and change the, the um, the value of it in the
module script, you then have to change in the script as well.
The whole idea of this script is so that no matter what you do to the module script, this always stays
the same.
It's allowing you to make changes without it disrupting your entire code.
Which is why this mod script really is so useful for this.
Um, so we're going to go in going to items frame.info dot cooldown label.
Cool down label dot text is equal to cool down.
Dot dot to string shop items, dot items, one dot cooldown and then just hit that one more time for
cost.
Just copy that.
Cost.
Okay.
Um, we'll also need to check to see what, um, the purchase button is going to be, because obviously,
if I've already got it, we don't we don't want it to be able to purchase it again.
So we'll be copying this bit of code here.
And implement product Productname will be, um, item stream.
The items and items frame shop items.
Shop items.
Items one dot name like that there.
Just in case you have purchased the first item, we don't want them to be able to purchase it again.
That's the whole point here.
And yeah, that should all that should fix the issue basically.
So if we go ahead and play the game, it should be just like we've clicked it even though we haven't.
And this will then this will be fixing our issue.
So we're loading in.
Let's click on the shop and see.
Just like we clicked it here really.
And can purchase it there.
Cool.
So we only bit we need to finish off now is with the purchase button.
So to do this we want it to we want to be able to purchase it when they click this purchase button here.
So let's go right down to the bottom of our code.
Add in another area called product purchasing.
Like this.
Um.
Oh.
Item stream.info.
Dot purchase button.
Mouse button.
One click.
Connect function.
Like this.
Here.
Um.
We're going to start off making a variable called product.
I'm making it equal to nil so that it updates from any other time.
We then need to find out what item they're trying to purchase.
And the best way to do this is well, the name label is going to have the name of the item.
So let's just fetch that and check through the shop items to find something that has the name of that.
So for each item.
In pairs.
Shop items.
Dot items.
If item.
Dot name.
Is equal to item stream.info dot name.
Label.
Dot text.
Then the product is equal to that item there.
And for simplicity here.
I'm going to make this one a function up at the top to purchase product.
Purchase product, which can be called.
The function can be called purchase product.
Now we don't want to run this function immediately.
We want to first check that they can purchase it.
They can only purchase it if they have the same amount of speed or more speed than what is required
to purchase this product.
So if speed dot value is bigger or equal to the product cost, then we want to run.
This function is called purchase product and it's going to have a parameter of the product.
We're wanting to try and purchase like that there.
Okay.
Let's then go back up to the top of our script and run and write out this function.
So, um, called item purchasing.
Function.
Okay.
So local function.
Purchase product.
Like this.
Why did make this a function?
I'm not too sure.
Really wanted to show you how functions can be properly used.
And there's a lot of.
I guess there's not too much code, but I think it just makes sense to kind of put it elsewhere, really.
But it's more so just so you can see an example of functions being used, the base functions, not the
events and stuff like that.
So we're going to first check if the purge button is active.
Because if we try to fire it and the purchase button is not active, we don't want it to work.
So if items frame.info.
Dot purchase button dot active.
Then we want to.
Well take away how much the item cost from their current speed.
So their speed dot value is equal to the current speed dot value that they have minus the cost of the
product.
I'm going to get that as a parameter which is passed over down here.
After that, we then want to disable the purchase button.
We're just going to be the exact same code.
Then here, when we realize that we've already purchased it, this exact same code here, we then want
to give the player a product.
The last little bit.
Now, as I mentioned, we can't really do this on the client here because if you give a player an item
on the client, they will only have it on their device, and for anyone else they won't have the item.
So it will be give.
It will give like um syncing issues between two players.
So in that case I'm going to have this run a separate function, write a separate script in server script
service, which will basically take it from here.
But this is going to be fired using the item purchased event here.
We haven't replicated storage that we added in last lesson.
So the item purchased colon Fire server server.
Go.
That's it for this shop.
System code here.
One little bit, one little more bit.
Um, in subscript service.
And we should be done.
Okay.
Um.
So let's head over to Server Script Service and add in a script.
This is going to be called Item Purchaser.
And inside here we want to first reference the event.
So local replicated.
Storage is equal to game get service, replicate storage.
Let's then get stopped.
This again.
Get the shop items module script and then the item purchased remote event.
So local shop items is equal to require not webgate storage um require.
Shop.
So, um, Rubbermaid storage shop items and then local.
Oh, don't know what happened there.
Local item purchased.
Is equal to replicate storage weight for child item purchased.
Okay, now, this is really simple.
All we need to do is clone the item and put it in their backpack.
And that's it.
Okay, so really simple script here.
Um, why is that?
Are close brackets.
That's the issue.
Okay.
Item purchased on server events connect to a function we want to pass over the player and the item they're
wanting to purchase.
Okay.
So the clone of item.
Is equal to the item dot two members from the the model script here.
Colon.
When item clone dot parent is equal to player dot backpack.
And there we go.
If this all works well, then we should have the shop working.
Just going and just try this out.
So, um, I put the first item here to cost ten speed, I believe.
Yeah, cost of ten.
I'm not going to walk around for ages.
I'm going to just update my value here in Leaderstats speeds to be ten.
And in here click Purchase perfect.
And we've now been given the item and our speed's been taken away.
Items down here.
And then we click this item.
We don't get any speed just yet because we still, we still need to code the item to give us speed,
which is the last little bit.
But we have now got our item, which is really cool.
We're holding that lollipop just there.
Um, perfect.
So that's the shop done.
So quite a big bit of code there, but hopefully you enjoyed seeing a larger code example.
Then we looked at in in our um, tutorial lectures.
But in the next lecture we are going to um, have a look at the stats display here, which we've kind
of, um, not really touched on, not not really touched on in the last few lectures and also code these
tools.
So these are giving us speed.
Once we've done that in the final lecture we are going to add in.
Um, so I add in some final touches and some other things and we'll be finished.
Really.
So we're getting towards the end of this speed simulator.
Okay.
So join me in the next lecture where we're going to have a go at the stats display and coding tools.